﻿using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using Styx.Helpers;
using Styx.WoWInternals;

namespace DbFirstAid
{
    public class DbFirstAidConfigSettings
    {
        string _cfgFile = "Settings";
        public DbFirstAidConfigSettings() 
        {
            try
            {
               
                Load();
            }
            catch (Exception)
            {  }
        }

        #region Variables
        public int useBandage = 75;
        public String linenBandage = "Disabled";
        public String woolenBandage = "Disabled";
        public String silkBandage = "Disabled";
        public String mageBandage = "Disabled";
        public String runeBandage = "Disabled";
        public String netherBandage = "Disabled";
        public String frostBandage = "Disabled";
        public String emberBandage = "Disabled";


        #endregion


        #region XmlUtils

        public void Load()
        {
            //    XmlTextReader reader;
            XmlDocument xml = new XmlDocument();
            XmlNode xvar;

            string sPath = Process.GetCurrentProcess().MainModule.FileName;
            sPath = Path.GetDirectoryName(sPath);
            sPath = Path.Combine(sPath, _cfgFile);

            if (!Directory.Exists(sPath))
            {
                Logging.WriteDebug("Creating config directory");
                Directory.CreateDirectory(sPath);
            }

            String serverName = Lua.GetReturnVal<string>("return GetRealmName()", 0);
            String toonName = ObjectManager.Me.Name;

            sPath = Path.Combine(sPath, "DBFirstAid-" + serverName + "-" + toonName + "-Settings.xml");


            //((Warlock)Global.CurrentCC).slog("");
            //Logging.Write("");

            Logging.WriteDebug("Loading config file: {0}", sPath);
            System.IO.FileStream fs = new System.IO.FileStream(@sPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
            try
            {
                xml.Load(fs);
                fs.Close();
            }
            catch (Exception e)
            {
                Logging.Write(e.Message);
                Logging.Write("Continuing with Default Config Values");
                fs.Close();
                return;
            }

        

            try
            {
                if (xml == null)
                    return;

                xvar = xml.SelectSingleNode("//DBFirstAid/useBandage");
                if (xvar != null)
                {
                    useBandage = Convert.ToInt16(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + useBandage.ToString());
                }

                //Resting Logic
                xvar = xml.SelectSingleNode("//DBFirstAid/linenBandage");
                if (xvar != null)
                {
                    linenBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + linenBandage.ToString());
                }

                xvar = xml.SelectSingleNode("//DBFirstAid/woolenBandage");
                if (xvar != null)
                {
                    woolenBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + woolenBandage.ToString());
                }

                //Shards And Stones
                xvar = xml.SelectSingleNode("//DBFirstAid/silkBandage");
                if (xvar != null)
                {
                    silkBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + silkBandage.ToString());
                }

                xvar = xml.SelectSingleNode("//DBFirstAid/mageBandage");
                if (xvar != null)
                {
                    mageBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + mageBandage.ToString());
                }

                xvar = xml.SelectSingleNode("//DBFirstAid/runeBandage");
                if (xvar != null)
                {
                    runeBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + runeBandage.ToString());
                }

                xvar = xml.SelectSingleNode("//DBFirstAid/netherBandage");
                if (xvar != null)
                {
                    netherBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + netherBandage.ToString());
                }

                xvar = xml.SelectSingleNode("//DBFirstAid/frostBandage");
                if (xvar != null)
                {
                    frostBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + frostBandage.ToString());
                }

                xvar = xml.SelectSingleNode("//DBFirstAid/emberBandage");
                if (xvar != null)
                {
                    emberBandage = Convert.ToString(xvar.InnerText);
                    Logging.WriteDebug("   cfg:  " + xvar.Name + "=" + emberBandage.ToString());
                }



            }
            catch (Exception e)
            {
                Logging.WriteDebug("DBFIRSTAID EXCEPTION, STACK=" + e.StackTrace);
                Logging.WriteDebug("DBFIRSTAID EXCEPTION, SRC=" + e.Source);
                Logging.WriteDebug("DBFIRSTAID : " + e.Message);
            }
        }


        public void Save()
        {
            XmlDocument xml;
            XmlElement root;
            XmlElement element;
            XmlText text;
            XmlComment xmlComment;

            String sPath = Process.GetCurrentProcess().MainModule.FileName;
            sPath = Path.GetDirectoryName(sPath);

            sPath = Path.Combine(sPath, _cfgFile);

            if (!Directory.Exists(sPath))
            {
                Logging.WriteDebug("Creating config directory");
                Directory.CreateDirectory(sPath);
            }

            String serverName = Lua.GetReturnVal<string>("return GetRealmName()", 0);
            String toonName = ObjectManager.Me.Name;
            

            sPath = Path.Combine(sPath, "DBFirstAid-" + serverName + "-" + toonName + "-Settings.xml");
            
            

            Logging.WriteDebug("Saving config file: {0}", sPath);
            xml = new XmlDocument();
            XmlDeclaration dc = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(dc);

            xmlComment = xml.CreateComment(
                "=======================================================================\n" +
                "Configuration file for the DBFirstAid Plugin\n\n" +
                "XML file containing settings to customize the the DBFirstAid Plugin.\n" +
                "It is STRONGLY recommended you use the Configuration UI to change this\n" +
                "file instead of direct changein it here.\n" +
                "========================================================================");

            //let's add the root element
            root = xml.CreateElement("DBFirstAid");
            root.AppendChild(xmlComment);

            //let's add another element (child of the root)
            element = xml.CreateElement("useBandage");
            text = xml.CreateTextNode(useBandage.ToString());
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("linenBandage");
            text = xml.CreateTextNode(linenBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("woolenBandage");
            text = xml.CreateTextNode(woolenBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("silkBandage");
            text = xml.CreateTextNode(silkBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("mageBandage");
            text = xml.CreateTextNode(mageBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("runeBandage");
            text = xml.CreateTextNode(runeBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("netherBandage");
            text = xml.CreateTextNode(netherBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("frostBandage");
            text = xml.CreateTextNode(frostBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            //let's add another element (child of the root)
            element = xml.CreateElement("emberBandage");
            text = xml.CreateTextNode(emberBandage);
            element.AppendChild(text);
            root.AppendChild(element);

            xml.AppendChild(root);

            //let's try to save the XML document in a file: C:\pavel.xml
            System.IO.FileStream fs = new System.IO.FileStream(@sPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            try
            {
                xml.Save(fs); 
                fs.Close();
            }
            catch (Exception e)
            {
                Logging.Write(e.Message);
            }



        }


        #endregion
    }
}
